﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.UI;
using FwProject.Core.Restaurant;
using FwProject.Restaurant;

namespace FwProject.Manager
{
    public class LoadCarRecoveryManager : IDomainService
    {
        private readonly IRepository<LoadCarRecovery, Guid> _loadCarRecoveryRepository;
        public LoadCarRecoveryManager(IRepository<LoadCarRecovery, Guid> loadCarRecoveryRepository)
        {
            _loadCarRecoveryRepository = loadCarRecoveryRepository;

        }
        /// <summary>
        /// 添加一个装车
        /// </summary>
        /// <param name="loadcar"></param>
        /// <returns></returns>
        public async Task<Guid> CreateLoadcar(LoadCarRecovery m)
        {

            var model = _loadCarRecoveryRepository.GetAll().Where(o =>o.LoadCarId== m.LoadCarId && o.TypeNumberInfoId==o.TypeNumberInfoId && o.RestaurantId==m.RestaurantId).FirstOrDefault();
            if (model != null) {
                m.Recoverycount = model.Recoverycount+ m.Recoverycount;
                m.Backboxcount = model.Backboxcount + m.Backboxcount;
                m.Backnumbercount= model.Backnumbercount + m.Backnumbercount;
                m.Damagecount= model.Backnumbercount + m.Backnumbercount;
            }
            return await _loadCarRecoveryRepository.InsertAndGetIdAsync(m);
        }
        public Guid CreateSync(LoadCarRecovery m)
        {

            var model = _loadCarRecoveryRepository.GetAll().Where(o => o.LoadCarId == m.LoadCarId && o.TypeNumberInfoId == o.TypeNumberInfoId && o.RestaurantId == m.RestaurantId).FirstOrDefault();
            if (model != null)
            {
                m.Recoverycount = model.Recoverycount + m.Recoverycount;
                m.Backboxcount = model.Backboxcount + m.Backboxcount;
                m.Backnumbercount = model.Backnumbercount + m.Backnumbercount;
                m.Damagecount = model.Backnumbercount + m.Backnumbercount;
            }
            return  _loadCarRecoveryRepository.InsertAndGetId(m);
        }

        /// <summary>
        ///修改装车信息
        /// </summary>
        /// <param name="loadcar"></param>
        /// <returns></returns>
        public async Task UpdateLoadcar(LoadCarRecovery loadcar)
        {

            await _loadCarRecoveryRepository.UpdateAsync(loadcar);
        }
        /// <summary>
        ///  delete 一个装车
        /// </summary>
        /// <param name="loadcar"></param>
        /// <returns></returns>
        public async Task DeleteLoadcar(LoadCarRecovery loadcar)
        {

            await _loadCarRecoveryRepository.DeleteAsync(loadcar);
        }
    }
}
